﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Persons.Classes;
using System.IO;

namespace PersonsWinForm
{
    public partial class FAddPerson : Form
    {
        public  Persona persona = null;
        ListOfPersons list = null;
        string AddCaptionForm = "Форма создания персоны";
        string EditCaptionForm = "Форма редактирования персоны";
        string msg = "";
        byte[] img=null;

        public FAddPerson(Persona per, ListOfPersons _list)
        {
            persona = per;
            list = _list;
            InitializeComponent();
        }

        private void Fill_Fields(object sender, EventArgs e)
        {
            if (persona != null)
            {
                this.Text = EditCaptionForm;
                TB_ID.Text = persona.ID.ToString();
                TB_FirstName.Text = persona.Fname;
                TB_LastName.Text = persona.Lname;
                TB_Age.Text = persona.Age.ToString();
                if (persona.Photo == null)
                {
                    PB_Photo.Image = global::PersonsWinForm.Properties.Resources.photo;
                }
                else
                {
                    MemoryStream memoryStream = new MemoryStream();
                    memoryStream.Write(persona.Photo, 0, persona.Photo.Length);
                    img = new byte[(int)memoryStream.Length];
                    img = persona.Photo;
                    PB_Photo.Image = Image.FromStream(memoryStream);
                }
                PB_Photo.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
                msg = "Сохранить отредактированные данные?";
            }
            else
            {
                this.Text = AddCaptionForm;
                TB_ID.Text = "";
                TB_FirstName.Text = "";
                TB_LastName.Text = "";
                TB_Age.Text = "";
                PB_Photo.Image = global::PersonsWinForm.Properties.Resources.photo;
                msg = "Создать новую персону?";
            }
        }

        private void FAddPerson_SizeChanged(object sender, EventArgs e)
        {
            this.Height = 400;
            this.Width = 420;
        }

        private void Btn_Save_Click(object sender, EventArgs e)
        {
            try
            {
                FSaveChanges fsc = new FSaveChanges(msg);
                fsc.ShowDialog(this);
                if (DialogResult.OK == fsc.DialogResult)
                {
                    if (persona != null)
                    {
                        list.Update(new Persona(Convert.ToInt32(TB_ID.Text), TB_FirstName.Text, TB_LastName.Text, Convert.ToInt32(TB_Age.Text),img));
                        persona = new Persona(Convert.ToInt32(TB_ID.Text), TB_FirstName.Text, TB_LastName.Text, Convert.ToInt32(TB_Age.Text),img);
                    }
                    else
                    {                        
                        list.Create(new Persona(Convert.ToInt32(TB_ID.Text), TB_FirstName.Text, TB_LastName.Text, Convert.ToInt32(TB_Age.Text), img));
                    }
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                this.DialogResult = DialogResult.None;
            }
        }

        private void TB_ID_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (Char)Keys.Enter)
            {
                TB_FirstName.Focus();
            }
            else
            {
                if (!(Char.IsDigit(e.KeyChar)))
                {
                    if (e.KeyChar != (char)Keys.Back)
                    {
                        e.Handled = true;
                    }
                }
            }
        }

        private void TB_FirstName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (Char)Keys.Enter)
            {
                TB_LastName.Focus();
            }
            else
            {
                if (!(Char.IsLetter(e.KeyChar))&&!(e.KeyChar==(Char)Keys.Space))
                {
                    if (e.KeyChar != (char)Keys.Back)
                    {
                        e.Handled = true;
                    }
                }
            }
        }

        private void TB_LastName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (Char)Keys.Enter)
            {
                TB_Age.Focus();
            }
            else
            {
                if (!(Char.IsLetter(e.KeyChar)) && !(e.KeyChar == (Char)Keys.Space))
                {
                    if (e.KeyChar != (char)Keys.Back)
                    {
                        e.Handled = true;
                    }
                }
            }
        }

        private void TB_Age_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (Char)Keys.Enter)
            {
                TB_Description.Focus();
            }
            else
            {
                if (!(Char.IsDigit(e.KeyChar)))
                {
                    if (e.KeyChar != (char)Keys.Back)
                    {
                        e.Handled = true;
                    }
                }
            }
        }

        private void TB_Description_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (Char)Keys.Enter)
            {
                Btn_Save.Focus();
            }
        }

        private void PB_Photo_Click(object sender, EventArgs e)
        {
            OpenFileDialog OD = new OpenFileDialog();
            OD.Filter = "PNG Files|*.png|JPEG Files|*jpg";
            if (OD.ShowDialog() == DialogResult.OK)
            {
                if ((OD.FileName != "") && (File.Exists(OD.FileName)))
                {
                    FileStream f = new FileStream(OD.FileName, FileMode.Open);
                    img = new byte[(int)f.Length];
                    Bitmap bm = new Bitmap(f);
                    f.Position = 0;
                    f.Read(img, 0, img.Length);
                    PB_Photo.Image = bm as Image;
                    PB_Photo.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
                    f.Close();
                }
            }
        }

        
    }
}
